home *** CD-ROM | disk | FTP | other *** search
- package horst;
-
- import java.applet.Applet;
- import java.applet.AppletContext;
- import java.applet.AppletStub;
- import java.applet.AudioClip;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.FontMetrics;
- import java.awt.Graphics;
- import java.awt.Image;
- import java.awt.Rectangle;
- import java.awt.Shape;
- import java.awt.Toolkit;
- import java.net.URL;
- import java.util.Enumeration;
- import java.util.Hashtable;
- import java.util.StringTokenizer;
- import java.util.Vector;
-
- public class AppletView extends ComponentView implements AppletStub, AppletContext {
- URL m_codeBaseURL;
- int m_width;
- int m_height;
- float m_pctWidth;
- float m_pctHeight;
- Hashtable m_params;
-
- public AppletView(View parent, Element e, HTMLPane container) {
- super(parent, e, container);
- }
-
- public void appletResize(int width, int height) {
- if (super.m_comp != null && (width >= 0 || height >= 0)) {
- super.m_comp.setSize(width, height);
- }
-
- }
-
- protected void flushResources() {
- if (super.m_comp != null) {
- ((Applet)super.m_comp).stop();
- ((Applet)super.m_comp).destroy();
- }
-
- }
-
- public final Applet getApplet(String name) {
- return null;
- }
-
- public final AppletContext getAppletContext() {
- return this;
- }
-
- public final Enumeration getApplets() {
- Vector v = new Vector();
- v.addElement(this);
- return v.elements();
- }
-
- public final AudioClip getAudioClip(URL url) {
- return null;
- }
-
- public final URL getCodeBase() {
- return this.m_codeBaseURL != null ? this.m_codeBaseURL : this.getDocumentBase();
- }
-
- public URL getDocumentBase() {
- return super.m_elem.getDocument().getBaseURL();
- }
-
- public final Image getImage(URL url) {
- return Toolkit.getDefaultToolkit().getImage(url);
- }
-
- protected int getMinimumSpan(int axis) {
- return this.getPreferredSpan(axis);
- }
-
- public final String getParameter(String name) {
- return (String)this.m_params.get(name);
- }
-
- protected int getPreferredSpan(int axis) {
- switch (axis) {
- case 0:
- if (this.m_height != -1) {
- return this.m_height;
- }
-
- return 100;
- case 1:
- if (this.m_width != -1) {
- return this.m_width;
- }
-
- return 100;
- default:
- return 100;
- }
- }
-
- protected void init() {
- this.m_width = 100;
- this.m_pctWidth = -1.0F;
- String value = (String)super.m_elem.getAttribute("width");
- if (value != null) {
- int idx = value.indexOf("%");
- if (idx > 0) {
- value = value.substring(0, idx);
- Integer iVal = Utilities.getInteger(value);
- if (iVal != null && iVal > 0 && iVal <= 100) {
- this.m_pctWidth = (float)iVal / 100.0F;
- this.m_width = -1;
- }
- } else {
- Integer iVal = Utilities.getInteger(value);
- this.m_width = iVal;
- }
- }
-
- this.m_height = 100;
- this.m_pctHeight = -1.0F;
- value = (String)super.m_elem.getAttribute("height");
- if (value != null) {
- int idx = value.indexOf("%");
- if (idx > 0) {
- value = value.substring(0, idx);
- Integer iVal = Utilities.getInteger(value);
- if (iVal != null && iVal > 0 && iVal <= 100) {
- this.m_pctHeight = (float)iVal / 100.0F;
- this.m_height = -1;
- }
- } else {
- Integer iVal = Utilities.getInteger(value);
- this.m_height = iVal;
- }
- }
-
- this.m_params = new Hashtable();
-
- for(int i = 0; i < super.m_elem.getElementCount(); ++i) {
- Element child = super.m_elem.getElementAt(i);
- if (child.getType() == 44) {
- String name = (String)child.getAttribute("name");
- value = (String)child.getAttribute("value");
- if (name != null && value != null) {
- this.m_params.put(name, value);
- }
- }
- }
-
- }
-
- public boolean isActive() {
- return true;
- }
-
- protected Rectangle layout(int x, int y, int width, LayoutInfo info) {
- int renderWidth;
- if ((double)this.m_pctWidth != (double)-1.0F) {
- renderWidth = (int)(this.m_pctWidth * (float)width);
- } else {
- renderWidth = this.getPreferredSpan(1);
- }
-
- super.m_bounds.setBounds(x, y, renderWidth, this.getPreferredSpan(0));
- if (super.m_comp != null) {
- super.m_comp.setBounds(super.m_bounds);
- }
-
- return super.m_bounds;
- }
-
- protected void loadApplet(String base, String className) {
- super.m_container.notifyStatusListeners(7, "Loading applet " + className + "...");
- Class appletClass = null;
-
- try {
- View.m_classLoader.setURLString(base);
- appletClass = View.m_classLoader.loadClass(className);
- } catch (Exception ex) {
- ((Throwable)ex).printStackTrace();
- return;
- }
-
- try {
- super.m_comp = (Component)appletClass.newInstance();
- Applet applet = (Applet)super.m_comp;
- applet.setStub(this);
- int initWidth = this.m_width != -1 ? this.m_width : 100;
- int initHeight = this.m_height != -1 ? this.m_height : 100;
- ((Component)applet).setSize(initWidth, initHeight);
- super.m_container.add(applet);
- applet.init();
- applet.start();
- } catch (Exception ex) {
- ((Throwable)ex).printStackTrace();
- }
- }
-
- protected void loadResources() {
- if (super.m_container.m_preferences.m_bLoadApplets) {
- String val = (String)super.m_elem.getAttribute("code");
- if (val != null) {
- String className = val;
- int idx = val.indexOf(".class");
- if (idx != -1) {
- className = val.substring(0, idx);
- }
-
- String base = className;
- URL baseURL = super.m_container.getDocument().getBaseURL();
- if (baseURL != null) {
- base = baseURL.toString();
- int lastBackSlash = base.lastIndexOf(47);
- int lastDot = base.lastIndexOf(46);
- if (lastDot > lastBackSlash) {
- base = base.substring(0, lastBackSlash);
- }
- }
-
- base = Utilities.removeTrailingBackslashes(base);
- String codebase = (String)super.m_elem.getAttribute("codebase");
- if (codebase != null) {
- boolean bRelative = true;
- if (!codebase.startsWith("/")) {
- URL testURL = Utilities.getURL(codebase);
- if (testURL != null) {
- bRelative = false;
- base = codebase;
- }
- }
-
- if (bRelative) {
- if (!codebase.startsWith("/")) {
- codebase = "/" + codebase;
- }
-
- base = base + codebase;
- }
-
- base = Utilities.removeTrailingBackslashes(base);
- }
-
- this.m_codeBaseURL = Utilities.getURL(base);
- String archive = (String)super.m_elem.getAttribute("archive");
- if (archive != null) {
- StringTokenizer tok = new StringTokenizer(archive, ", ", false);
-
- while(tok.hasMoreTokens()) {
- String file = tok.nextToken();
- if (file.endsWith(".jar") || file.endsWith(".JAR") || file.endsWith(".zip") || file.endsWith(".ZIP")) {
- try {
- View.m_classLoader.loadJAR(base, file);
- } catch (Exception e) {
- ((Throwable)e).printStackTrace();
- return;
- }
- }
- }
- }
-
- this.loadApplet(base, className);
- }
-
- }
- }
-
- public void paint(Graphics g, Shape alloc) {
- if (super.m_comp == null) {
- Color oldColor = g.getColor();
- g.setColor(Color.blue);
- g.drawRect(super.m_bounds.x, super.m_bounds.y, super.m_bounds.width, super.m_bounds.height);
- String msg = "Applet missing!";
- FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(((View)this).getFont());
- int strWidth = fm.stringWidth(msg);
- if (strWidth <= super.m_bounds.width && fm.getHeight() <= super.m_bounds.height) {
- int offset = (super.m_bounds.width - strWidth) / 2;
- g.drawString(msg, super.m_bounds.x + offset, super.m_bounds.y + fm.getHeight());
- }
-
- g.setColor(oldColor);
- }
-
- ((View)this).drawDebugBox(g, Color.blue);
- }
-
- public void showDocument(URL url) {
- super.m_container.openPage(url);
- }
-
- public void showDocument(URL url, String target) {
- }
-
- public void showStatus(String text) {
- super.m_container.notifyStatusListeners(7, text);
- }
- }
-